package com.example.Lezedata.ui.fragment.goodsDetail;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.BidDetailManager;
import com.example.Lezedata.dataModel.GoodsProjectModel;
import com.example.Lezedata.dataModel.ProgressModel;
import com.example.Lezedata.ui.activity.goods.GoodsNameView;
import com.example.Lezedata.ui.activity.goods.ProgressView;
import com.example.Lezedata.ui.fragment.BaseFragment;
import com.example.Lezedata.util.GBInterface;
import com.example.Lezedata.util.GBTimeManager;
import com.example.Lezedata.util.GBUtil;
import com.example.Lezedata.util.RadiusBackgroundSpan;

import java.sql.Time;
import java.util.Date;

import bid.Bid;

import static com.example.Lezedata.GBApplication.getContext;

public class GoodsDetailHeadViewFragment extends BaseFragment {

    public GBInterface viewCreatedListener;
    public GoodsProjectModel goodsProjectModel;
    public Bid.BidDetailResponse detailResponse;

    private long dueTime;

    ImageView goodsIcon;
    TextView priceTextView;
    TextView TimeTextView;
    GoodsNameView goodsNameView;
    ProgressView progressView;

    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            refreshTime(dueTime);
            Log.d(TAG, "run: ");
            handler.postDelayed(this, 1000);
        }
    };

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.goods_detail_head_view_fragment, container, false);
    }

    @Override
    public void initView() {
        super.initView();
        if (viewCreatedListener != null){
            viewCreatedListener.onViewCreated(contentView);
        }
        goodsIcon = findViewById(R.id.pro_goods_icon);
        progressView = findViewById(R.id.pro_progress);
        goodsNameView = findViewById(R.id.pro_goodsName);
        priceTextView = findViewById(R.id.pro_price);
        TimeTextView = findViewById(R.id.pro_time);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // TODO: Use the ViewModel
    }

    public void setDetailResponse(Bid.BidDetailResponse detailResponse) {
        this.detailResponse = detailResponse;
        refreshUIForModel(detailResponse);
    }

    public void refreshUIForModel(final Bid.BidDetailResponse model) {
        BidDetailManager.getGoodsInfoForId(model.getGoodsId(), new BidDetailManager.OnLoadDataComplete() {
            @Override
            public void infoResponse(Bid.GoodsInfo goodsInfo) {
                GBUtil.setGoodsImage(goodsIcon,goodsInfo.getImgUrl());
//                goodsNameView.setModelForNameWithDetail(GBUtil.RoundToString(model.getBidRound()),goodsInfo.getName());
            }
        });

        ProgressModel progressModel = new ProgressModel();
        progressModel.total = model.getTotalBid();
        progressModel.current = model.getTotalBid() - model.getRemainBid();
        progressView.setModel(progressModel);

        String s1 = String.valueOf(model.getPrice());
        String s2 = s1 + "NGN/Bid";
        SpannableString string = GBUtil.textAddFont(s2,35,0,s1.length());
        priceTextView.setText(string);

        dueTime = model.getExpireTime();
        refreshTime(dueTime);
        runnable.run();
    }

    @SuppressLint("DefaultLocale")
    public void refreshTime(long time){

        Date date = new Date();
        long currentSeconds = date.getTime()/1000;
        long stime = time - currentSeconds;
        if (stime < 0){
            stime = 0;
            handler.removeCallbacks(runnable);
        }
        int day = (int)(stime/86400);
        int dueSeconds = (int)stime%86400;

        int hour = dueSeconds/3600;
        int minutes = dueSeconds%3600/60;
        int seconds = dueSeconds%60;

        String str1 = String.format("%02d",day);
        String str2 = String.format("%02d",hour);
        String str3 = String.format("%02d",minutes);
        String str4 = String.format("%02d",seconds);

        String str5 = String.format("%s:%s:%s:%s",str1,str2,str3,str4);

        int index1 = 0;
        int index2 = str1.length() + 1;
        int index3 = index2 + str2.length() + 1;
        int index4 = index3 + str3.length()+ 1;

        SpannableString spannableString = new SpannableString(str5);
        addBackgroundColorToText(spannableString,index1,str1.length());
        addBackgroundColorToText(spannableString,index2,str2.length());
        addBackgroundColorToText(spannableString,index3,str3.length());
        addBackgroundColorToText(spannableString,index4,str4.length());
        TimeTextView.setText(spannableString);
    }

    void addBackgroundColorToText (SpannableString spannableString , int start , int length){
        StyleSpan styleSpan  = new StyleSpan(Typeface.BOLD);
        RadiusBackgroundSpan backgroundSpan = new RadiusBackgroundSpan(Color.parseColor("#F3B72C"),GBUtil.dpTo2px(getContext(),5),Color.WHITE);
        spannableString.setSpan(styleSpan, start , start + length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(backgroundSpan, start , start + length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        handler.removeCallbacks(runnable);
    }
}